from flask_restful import abort
from sqlalchemy.orm import validates

from comm.sqlalchemyConfig import db
from model.base import BaseModel


class xsjAuthorModel(BaseModel):
    """
    用户表
    """
    __tablename__ = 'xsj_author'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='id')
    name = db.Column(db.String(125), nullable=False,unique=True,doc='名称')
    is_del = db.Column(db.Boolean, nullable=True,default=False,doc='是否删除')
    rem = db.Column(db.Text, nullable=True,default=None,doc='备注')

    @validates('name')
    def validate_address(self, key, name):
        existData = self.__class__.query.filter_by(name=name).first()
        if existData != None:
            abort(400, msg='用户名重复')
        return name

    def __str__(self):
        return 'xsj_author:{}'.format(self.name)


class xsjAuthor1Model(BaseModel):
    """
    用户表
    """
    __tablename__ = 'xsj_author1'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='id')
    name = db.Column(db.String(125), nullable=False,unique=True,doc='名称')
    is_del = db.Column(db.Boolean, nullable=True,default=False,doc='是否删除')
    atm = db.Column(db.Numeric(20, 6), nullable=True, default=0, doc='金额')
    atm1 = db.Column(db.Float(), nullable=True, default=0, doc='金额')
    atm2 = db.Column(db.Integer, nullable=True, default=0, doc='金额')
    rem = db.Column(db.Text, nullable=True,default=None,doc='备注')
    update_time1 = db.Column(db.Time, nullable=True)
    update_time2 = db.Column(db.Date, nullable=True)

    @classmethod
    def columnConstructor(cls):
        """
        用于模型的一些特殊处理
        :return:
        """
        # 需要显示的字段
        showColumn = []
        # 不需要显示的字段
        hiddenColumn = [""]
        # 组件字段
        primaryKeyColumn = ["id"]
        # 接口是否需要登录验证
        loginRequired = ["getList","post","put","getPk","delete"]
        dataObj = {}
        dataObj["showColumn"] = showColumn
        dataObj["hiddenColumn"] = hiddenColumn
        dataObj["primaryKeyColumn"] = primaryKeyColumn
        dataObj["loginRequired"] = loginRequired
        return dataObj




